from resource_management.core.resources.system import Execute
from resource_management.libraries.script import Script
from resource_management.libraries.functions import get_user_call_output
from resource_management.core.resources.system import Directory
from resource_management.core.resources.system import File
from resource_management.core.source import InlineTemplate
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.core.exceptions import ClientComponentHasNoStatus
from resource_management.libraries.functions.show_logs import show_logs

import os

class Engine(Script):
    pid_file=''
    def install(self, env):
        import params
        env.set_params(params)
        self.install_packages(env)
        Directory([
            params.install_dir, params.mlsql_log_dir, params.mlsql_pid_dir

        ],
            owner=params.mlsql_user,
            group=params.user_group,
            mode=0755,
            create_parents=True)

        if not os.path.exists(params.install_dir + '/mlsql-engine'):
            if not os.path.exists(params.download_engine_file):
                Execute(format('rm -f ' + params.download_engine_file), user=params.mlsql_user)
                Execute(format('wget ' + params.download_url_engine + ' -O ' + params.download_engine_file),
                        user=params.mlsql_user)
            Execute('rm -rf '+ params.install_dir +'/mlsql-engine* ', user=params.mlsql_user)
            # install mlsql

            tar_cmd = format('tar zxvf ' + params.download_engine_file + '   -C ' + params.install_dir)
            Execute(tar_cmd, user=params.mlsql_user)
            # Execute('tar zxvf /tmp/mlsql-engine.tar.gz   -C /opt/mlsql' ,user=params.mlsql_user)
            Execute('ln -s '+ params.install_dir +'/mlsql-engine* '+ params.install_dir +'/mlsql-engine', user=params.mlsql_user)

    def configure(self, env, isInstall=False):
        import params
        import status_params
        env.set_params(params)
        env.set_params(status_params)
        Directory([
            params.install_dir, params.mlsql_log_dir, params.mlsql_pid_dir,
            params.install_dir + '/mlsql'

        ],
            owner=params.mlsql_user,
            group=params.user_group,
            mode=0755,
            create_parents=True)
        File(os.path.join(params.install_dir + '/mlsql-engine', "start-default.sh"),
             content=InlineTemplate(params.engine_conf_start_default_sh),
             owner=params.mlsql_user,
             group=params.user_group
             )
        File(os.path.join(params.install_dir + '/mlsql-engine', "start-local.sh"),
             content=InlineTemplate(params.engine_conf_start_local_sh),
             owner=params.mlsql_user,
             group=params.user_group
             )
        File(os.path.join(params.install_dir + '/mlsql', "mlsql-engine.sh"),
             content=InlineTemplate(params.mlsql_engine_script),
             owner=params.mlsql_user,
             group=params.user_group
             )

    def stop(self, env):
        import params
        Execute(format('sh '+ params.install_dir +'/mlsql/mlsql-engine.sh stop'), user=params.mlsql_user)
    
        
    def start(self, env):
        import params
        import time

        env.set_params(params)
        self.configure(env)
        engine_start_cmd = format("export JAVA_HOME="+ params.java_home + " && export SPARK_HONE=" + params.spark_home + " && sh "+ params.install_dir +"/mlsql/mlsql-engine.sh start")
        Execute(engine_start_cmd, user=params.mlsql_user)
        # Execute('sh /opt/mlsql/mlsql/mlsql-engine.sh start',user=params.mlsql_user)
        time.sleep(5)
        Execute(format('sh '+ params.install_dir +'/mlsql/mlsql-engine.sh status'), user=params.mlsql_user)

    def status(self, env):
        import status_params
        env.set_params(status_params)
        check_process_status(status_params.mlsql_engine_pid_file)


    def get_pid_files(self):
        import status_params
        return [status_params.mlsql_engine_pid_file]

    def get_log_folder(self):
        import params
        return params.log_dir

    def get_user(self):
        import params
        return params.mlsql_user
          
if __name__ == "__main__":
    Engine().execute()
